<template>
  <div class="bed-view-container">
    <el-card shadow="never" class="page-card">
      <template #header>
        <div class="card-header">
          <span>床位示意图</span>
        </div>
      </template>

      <!-- Filters and Stats -->
      <div class="header-controls">
        <el-form :inline="true" class="filter-form">
          <el-form-item label="楼层:">
            <el-select v-model="selectedFloor" placeholder="请选择楼层" @change="handleFloorChange">
              <el-option
                v-for="floor in floors"
                :key="floor"
                :label="`${floor}楼`"
                :value="floor"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="stats-container">
          <span class="stat-item">
            <i class="stat-icon" style="background-color: #409EFF;"></i>
            <span>总床数: {{ stats.total }}</span>
          </span>
          <span class="stat-item">
            <i class="stat-icon" style="background-color: #67C23A;"></i>
            <span>空闲: {{ stats.available }}</span>
          </span>
          <span class="stat-item">
            <i class="stat-icon" style="background-color: #F56C6C;"></i>
            <span>有人: {{ stats.occupied }}</span>
          </span>
           <span class="stat-item">
            <i class="stat-icon" style="background-color: #E6A23C;"></i>
            <span>外出: {{ stats.outing }}</span>
          </span>
        </div>
      </div>

      <!-- Room Grid -->
      <div class="room-grid-container" v-loading="loading">
        <div class="room-grid">
          <div
            v-for="room in paginatedRooms"
            :key="room.id"
            :class="['room-card', `room-type-${room.type}`]"
          >
            <div class="room-header">
              {{ room.roomNumber }}
            </div>
            <div class="room-content">
              <!-- Utility Room -->
              <div v-if="room.type !== 'ward'" :class="['utility-room-content', `utility-${room.name}`]">
                <span>{{ room.name }}</span>
              </div>
              <!-- Ward Room -->
              <div v-else class="ward-room-content">
                <div v-for="bed in room.beds" :key="bed.id" class="bed-wrapper">
                  <div :class="['bed-icon', `bed-status-${bed.status}`]"></div>
                  <div class="bed-number">{{ bed.bedNumber }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

       <!-- Pagination -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[15, 30, 45, 60]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalRoomsOnFloor"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { inject, ref, reactive, computed, onMounted } from 'vue';
import { ElMessage } from 'element-plus';

// 路由与 axios
const axios = inject('axios');

const loading = ref(false);
const selectedFloor = ref(1);
const floors = ref([]);

const currentPage = ref(1);
const pageSize = ref(15);

// 真实数据
const allRooms = reactive([]);
const floorStats = reactive({ total: 0, available: 0, occupied: 0, outing: 0 });

// 获取所有楼层
const loadFloors = async () => {
  try {
    const response = await axios.get('/RoomInfoController/queryAllFloors');
    if (response.data.status === 200) {
      floors.value = response.data.data;
      if (floors.value.length > 0) {
        selectedFloor.value = floors.value[0];
      }
    } else {
      ElMessage.error('获取楼层信息失败');
    }
  } catch (error) {
    console.error('获取楼层信息失败:', error);
    ElMessage.error('获取楼层信息失败');
  }
};

// 获取指定楼层的房间和床位信息
const loadRoomsByFloor = async (floor) => {
  loading.value = true;
  try {
    const response = await axios.get('/RoomInfoController/getRoomsByFloor', {
      params: { floor }
    });
    if (response.data.status === 200) {
      allRooms.splice(0, allRooms.length, ...response.data.data);
    } else {
      ElMessage.error('获取房间信息失败');
    }
  } catch (error) {
    console.error('获取房间信息失败:', error);
    ElMessage.error('获取房间信息失败');
  } finally {
    loading.value = false;
  }
};

// 获取楼层统计信息
const loadFloorStats = async (floor) => {
  try {
    const response = await axios.get('/RoomInfoController/getFloorStats', {
      params: { floor }
    });
    if (response.data.status === 200) {
      Object.assign(floorStats, response.data.data);
    } else {
      ElMessage.error('获取统计信息失败');
    }
  } catch (error) {
    console.error('获取统计信息失败:', error);
    ElMessage.error('获取统计信息失败');
  }
};

onMounted(async () => {
  await loadFloors();
  if (selectedFloor.value) {
    await loadRoomsByFloor(selectedFloor.value);
    await loadFloorStats(selectedFloor.value);
  }
});

const roomsOnSelectedFloor = computed(() => {
  return allRooms.filter(room => room.floor === selectedFloor.value);
});

const totalRoomsOnFloor = computed(() => roomsOnSelectedFloor.value.length);

const paginatedRooms = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return roomsOnSelectedFloor.value.slice(start, end);
});

const stats = computed(() => floorStats);

const handleFloorChange = async () => {
  currentPage.value = 1;
  await loadRoomsByFloor(selectedFloor.value);
  await loadFloorStats(selectedFloor.value);
};

const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
};

</script>

<style scoped>
.bed-view-container {
  padding: 20px;
  background-color: #f7f8fa;
}
.page-card {
  border-radius: 8px;
  border: none;
  background-color: #fff;
}
.card-header {
  font-size: 18px;
  font-weight: 500;
  text-align: center;
  color: #303133;
}
.header-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 10px;
  flex-wrap: wrap;
}
.filter-form .el-form-item {
  margin-bottom: 0;
}
.stats-container {
  display: flex;
  align-items: center;
  gap: 25px;
  font-size: 14px;
}
.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
}
.stat-icon {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.room-grid-container {
  min-height: 500px;
}

.room-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 20px;
}

.room-card {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background-color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: box-shadow 0.3s;
  height: 180px;
}
.room-card:hover {
  box-shadow: 0 6px 18px rgba(0,0,0,0.08);
  transform: translateY(-2px);
}

.room-header {
  padding: 8px 12px;
  font-weight: bold;
  color: #606266;
  text-align: center;
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
}

.room-content {
  padding: 10px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.room-type-utility {
  background-color: #fff;
}
.utility-room-content {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-grow: 1;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  color: #fff;
}

.utility-活动中心 { background-color: #2ecc71; }
.utility-电梯厅 { background-color: #3498db; }
.utility-洗衣房 { background-color: #9b59b6; }
.utility-走道 { background-color: #f39c12; }


.ward-room-content {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  justify-items: center;
  align-items: center;
  height: 100%;
}

.bed-wrapper {
  text-align: center;
  width: 100%;
}

.bed-icon {
  width: 40px;
  height: 25px;
  margin: 0 auto 4px;
  border-radius: 4px;
}

/* 更新后的床位状态颜色，颜色更深、区别更明显 */
.bed-status-available { background-color: #67C23A; }
.bed-status-occupied { background-color: #F56C6C; }
.bed-status-outing    { background-color: #E6A23C; }

.bed-number {
  font-size: 12px;
  color: #909399;
  transform: scale(0.9);
}

.pagination-container {
  margin-top: 25px;
  display: flex;
  justify-content: center;
}
</style> 